package org.jeecg.modules.rooms.controller;


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.Enum.ResultCode;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.business.enums.CustomerTypeEnum;
import org.jeecg.modules.rooms.DTO.CesHourRoomRuleModifyDto;
import org.jeecg.modules.rooms.DTO.CesHousePriceSchemeDto;
import org.jeecg.modules.rooms.DTO.CesHousePriceSchemeSearchDto;
import org.jeecg.modules.rooms.DTO.CesRoomLayoutDto;
import org.jeecg.modules.rooms.entity.CesHousePriceScheme;
import org.jeecg.modules.rooms.service.CesHousePriceSchemeServiceImpl;
import org.jeecg.modules.rooms.util.Pinyin4jUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>
 * 房价方案 前端控制器
 * </p>
 *
 * @author Claude
 * @since 2023-03-09
 */
@RestController
@RequestMapping("/rooms/cesHousePriceScheme")
@Api(tags = "ces_house_price_scheme")
@Slf4j
public class CesHousePriceSchemeController extends JeecgController<CesHousePriceScheme, CesHousePriceSchemeServiceImpl> {

    @Resource
    CesHousePriceSchemeServiceImpl cesHousePriceSchemeService;

    @ApiOperation(value="房价方案查询", notes="房价方案查询")
    @GetMapping(value = "/list")
    public Result list(CesHousePriceSchemeSearchDto dto){
        if (ObjectUtils.isEmpty(dto)){
            return Result.error(ResultCode.PARAM_MISS);
        }
        if (0>dto.getPageNo()){
            return Result.error("页数不能小于等于0");
        }
        if (0>=dto.getPageSize()){
            return Result.error("页码尺寸不能小于等于0");
        }
        if (StringUtils.isBlank(dto.getHotelId())){
            return Result.error(ResultCode.PARAM_MISS);
        }
        try{
            return cesHousePriceSchemeService.list(dto);
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }


    @ApiOperation(value="房价方案查询", notes="房价方案查询")
    @GetMapping(value = "/findAcquiesceScheme")
    public Result<CesHousePriceScheme> findAcquiesceScheme(Integer type, String hotelId){
        if (ObjectUtil.isEmpty(type) || StrUtil.isEmpty(hotelId)){
            return Result.error("参数异常");
        }
        CesHousePriceScheme scheme = cesHousePriceSchemeService.getOne(Wrappers.<CesHousePriceScheme>lambdaQuery()
                .eq(CesHousePriceScheme::getSchemeType, type).eq(CesHousePriceScheme::getHotelId, hotelId).last("limit 1"));
        if (scheme == null){
            return Result.error("参数异常");
        }
//        String _checkText = scheme.getCheckInTime() == 0?"无限制":(scheme.getStartTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))+" - "
//                +scheme.getEndTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
//        scheme.setCheckInTimeText(_checkText);
//        String _validText = scheme.getValidTime() == 0?"无限制":(scheme.getValidStartTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))+" - "
//                +scheme.getValidEndTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
//        scheme.setValidTimeText(_validText);
        try{
            return Result.ok(scheme);
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

    @ApiOperation(value="房价方案查询", notes="房价方案查询")
    @GetMapping(value = "/listEnum")
    public Result listEnum(CesHousePriceSchemeSearchDto dto){
        return cesHousePriceSchemeService.listEnum(dto);
    }

    @ApiOperation(value="房价方案查询", notes="房价方案查询")
    @GetMapping(value = "/listByLayout")
    public Result listByLayout(CesHousePriceSchemeSearchDto dto){
        if (ObjectUtils.isEmpty(dto)){
            return Result.error(ResultCode.PARAM_MISS);
        }
        if (StringUtils.isBlank(dto.getHotelId())){
            return Result.error(ResultCode.PARAM_MISS);
        }
        if (StringUtils.isBlank(dto.getLayoutId())){
            return Result.error(ResultCode.PARAM_MISS);
        }
        try{
            return cesHousePriceSchemeService.listByLayout(dto);
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

    @AutoLog(value = "新增房价方案")
    @ApiOperation(value="新增房价方案", notes="新增房价方案")
    @PostMapping(value = "/save")
    public Result save(@RequestBody CesHousePriceSchemeDto dto){
        if (ObjectUtils.isEmpty(dto)){
            return Result.error(ResultCode.PARAM_MISS);
        }
        if (StringUtils.isBlank(dto.getPriceScheme())){
            return Result.error(ResultCode.PARAM_MISS);
        }
        String _code = Pinyin4jUtil.toPinYinLowercase(dto.getPriceScheme());
        dto.setSimpleCode(_code);
//        if (StringUtils.isBlank(dto.getSimpleCode())){
//            return Result.error(ResultCode.PARAM_MISS);
//        }
//        if (StringUtils.isBlank(dto.getNumber())){
//            return Result.error(ResultCode.PARAM_MISS);
//        }
//        if (null==dto.getGuestType()){
//            return Result.error(ResultCode.PARAM_MISS);
//        }
        if (null==dto.getCheckInTime()){
            return Result.error(ResultCode.PARAM_MISS);
        }else{
            if (1==dto.getCheckInTime().intValue()){
                //如果入住时间为时间段,就要判断时间的非空
                if (null==dto.getStartTime()){ return Result.error(ResultCode.PARAM_MISS); }
                if (null==dto.getEndTime()){ return Result.error(ResultCode.PARAM_MISS); }
            }
        }
        if (null==dto.getValidTime()){
            return Result.error(ResultCode.PARAM_MISS);
        }else{
            if (1==dto.getValidTime().intValue()){
                //如果有效期为时间段,就要判断时间的非空
                if (null==dto.getValidStartTime()){ return Result.error(ResultCode.PARAM_MISS); }
                if (null==dto.getValidEndTime()){ return Result.error(ResultCode.PARAM_MISS); }
            }
        }
        if (null==dto.getOpen()||(0!=dto.getOpen().intValue()&&1!=dto.getOpen().intValue())){
            return Result.error(ResultCode.PARAM_MISS);
        }
//        if (null==dto.getSort()||1>dto.getSort().intValue()){
//            return Result.error("排序不能为空且不能小于1！");
//        }
        try{
            return cesHousePriceSchemeService.create(dto);
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

    @AutoLog(value = "修改房价方案")
    @ApiOperation(value="修改房价方案", notes="修改房价方案")
    @PutMapping(value = "/modify")
    public Result modify(@RequestBody CesHousePriceSchemeDto dto){
        if (ObjectUtils.isEmpty(dto)){
            return Result.error(ResultCode.PARAM_MISS);
        }
        if(StringUtils.isBlank(dto.getId())){
            return Result.error(ResultCode.PARAM_MISS);
        }
        if(StringUtils.isBlank(dto.getHotelId())){
            return Result.error(ResultCode.PARAM_MISS);
        }
        //todo  zq:房价方案可能有问题
//        if (StringUtils.isBlank(dto.getPriceScheme())){
//            return Result.error(ResultCode.PARAM_MISS);
//        }
//        String _code = Pinyin4jUtil.toPinYinLowercase(dto.getPriceScheme());
//        dto.setSimpleCode(_code);
//        if (StringUtils.isBlank(dto.getSimpleCode())){
//            return Result.error(ResultCode.PARAM_MISS);
//        }
//        if (dto.getType() == 3 && StringUtils.isBlank(dto.getNumber())){
//            return Result.error(ResultCode.PARAM_MISS);
//        }
//        if (null==dto.getGuestSource()){
//            return Result.error(ResultCode.PARAM_MISS);
//        }
//        if (null==dto.getGuestType()){
//            return Result.error(ResultCode.PARAM_MISS);
//        }
        if (null==dto.getCheckInTime()){
            return Result.error(ResultCode.PARAM_MISS);
        }else{
            if (1==dto.getCheckInTime().intValue()){
                //如果入住时间为时间段,就要判断时间的非空
                if (null==dto.getStartTime()){ return Result.error(ResultCode.PARAM_MISS); }
                if (null==dto.getEndTime()){ return Result.error(ResultCode.PARAM_MISS); }
            }
        }
        if (null==dto.getValidTime()){
            return Result.error(ResultCode.PARAM_MISS);
        }else{
            if (1==dto.getValidTime().intValue()){
                //如果有效期为时间段,就要判断时间的非空
                if (null==dto.getValidStartTime()){ return Result.error(ResultCode.PARAM_MISS); }
                if (null==dto.getValidEndTime()){ return Result.error(ResultCode.PARAM_MISS); }
            }
        }
        if (null==dto.getOpen()||(0!=dto.getOpen().intValue()&&1!=dto.getOpen().intValue())){
            return Result.error(ResultCode.PARAM_MISS);
        }
//        if (dto.getType() == 3 && (null==dto.getSort()||1>dto.getSort().intValue())){
//            return Result.error("排序不能为空且不能小于1！");
//        }
        if(dto.getGuestType() != null && CustomerTypeEnum.val(dto.getGuestType()) == null){
            throw new JeecgBootException("客人类型不正确");
        }
        try{
            return  cesHousePriceSchemeService.modify(dto);
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

    @AutoLog(value = "修改上架状态")
    @ApiOperation(value="修改上架状态", notes="修改上架状态")
    @PutMapping(value = "/modifyOpen/{id}")
    public Result modifyOpen(@PathVariable String id, @RequestParam String hotelId, @RequestParam Integer open){
        if(StringUtils.isBlank(id)){
            return Result.error(ResultCode.PARAM_MISS);
        }
        if(StringUtils.isBlank(hotelId)){
            return Result.error(ResultCode.PARAM_MISS);
        }
        if(open < 0){
            return Result.error(ResultCode.PARAM_MISS);
        }
        try{
            return  cesHousePriceSchemeService.modifyOpen(id,hotelId, open);
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

    @AutoLog(value = "删除一条房价方案")
    @ApiOperation(value="删除一条房价方案", notes="删除一条房价方案")
    @DeleteMapping(value = "/delete")
    public Result delete(@RequestParam String id){
        if(StringUtils.isBlank(id)){
            return Result.error(ResultCode.PARAM_MISS,"数据ID必传!");
        }
        try{
            return  cesHousePriceSchemeService.deleteOne(id);
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

    @AutoLog(value = "批量删除房价方案")
    @ApiOperation(value="批量删除房价方案", notes="批量删除房价方案")
    @DeleteMapping(value = "/batchDelete")
    public Result batchDelete(@RequestParam String idStr){
        if (StringUtils.isBlank(idStr)){
            return Result.error(ResultCode.PARAM_MISS,"数据IDSTR必传!");
        }
        try{
            List<String> ids = Stream.of(idStr.split(",")).collect(Collectors.toList());
            return  cesHousePriceSchemeService.batchDelete(ids);
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

    /**
     *  批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "房价方案-批量删除")
    @ApiOperation(value="房价方案-批量删除", notes="房价方案-批量删除")
    //@RequiresPermissions("business:bus_advert_info:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
        try{
            List<String> _ids = Stream.of(ids.split(",")).collect(Collectors.toList());
            return  cesHousePriceSchemeService.batchDelete(_ids);
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

}

